/*
 * StockTableContentPane.java
 *
 * Created on December 11, 2007, 9:39 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.fie.contentpanes;

import com.fie.database.Connector;
import com.fie.graphing.DynamicGraph;
import com.sun.org.apache.xerces.internal.dom3.as.ASAttributeDeclaration;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

/**
 *
 * @author joseacuna-rohter
 */
public class StockTableContentPane extends javax.swing.JFrame {
    
    private JButton addStockButton;
    private JButton removeStockButton;
    private JTextField inputStock;
    private JLabel tickerLabel;
    private JPanel totalGUI;
   
    private TablePane tp;
    private int customerId;
    private Connector c;
    private JFrame buySellFrame;
    private JFrame newsFeedFrame;
    private JFrame activePositionsFrame;
    
    private BuySellPane bsp;
    private ActivePositionsPane app;
    private NewsContentPane ncp;
    
    /** Creates a new instance of StockTableContentPane */
    public StockTableContentPane(int customerId) {
        this.customerId = customerId;
        

        
        
        app = new ActivePositionsPane(customerId);
        activePositionsFrame = new JFrame("Active Positions");
        activePositionsFrame.setContentPane(app.createContentPane());
        activePositionsFrame.setSize(750,525);
        activePositionsFrame.setLocation(0,320);
        
        
        //Buy Sell Pane
        buySellFrame = new JFrame("Buy or Sell Position");        
        bsp = new BuySellPane(customerId,app);
        buySellFrame.setContentPane(bsp.createContentPane());
        buySellFrame.setSize(650, 300);
        buySellFrame.setLocation(750,0);
        
        //News Feed Pane
        /*
        newsFeedFrame = new JFrame("News Feed");        
        ncp = new NewsContentPane(customerId,app);
        newsFeedFrame.setContentPane(ncp.createContentPane());
        newsFeedFrame.setSize(650, 300);
        newsFeedFrame.setLocation(0,610);
        newsFeedFrame.setVisible(true);
        */
        
        //Graphing
        DynamicGraph dg = new DynamicGraph("Real-Time Graph");
        dg.setSize(650,300);
        dg.setLocation(750,320);
        dg.setVisible(true);
        
        activePositionsFrame.setVisible(true);
        buySellFrame.setVisible(true);
        
    }
    
    
    

    
    
    
    
    public Container createContentPane()
    {
        
       
        // We create a bottom JPanel to place everything on.
        totalGUI = new JPanel();
        
        // We set the Layout Manager to null so we can manually place
        // the Panels.
        totalGUI.setLayout(null);
        
        // Now we create a new panel, size it, shape it,color it red.
        // then add it to the bottom JPanel.
        JPanel redPanel = new JPanel();
        redPanel.setBackground(Color.white);
        redPanel.setLocation(10, 10);
        redPanel.setSize(200,50);
        
        addStockButton = new JButton("Add New Stock");
        addStockButton.setBackground(Color.white);
        addStockButton.setHorizontalAlignment(0);
        redPanel.add(addStockButton);
        totalGUI.add(redPanel);

        
        //Have the input text field here
        //AND also the tickerLabel
        JPanel textFieldPanel = new JPanel();
        textFieldPanel.setBackground(Color.white);
        textFieldPanel.setLocation(220,10);
        textFieldPanel.setSize(500,50);
        
        inputStock = new JTextField(5);
        inputStock.setHorizontalAlignment(0);
        tickerLabel = new JLabel("Symbol:");
        tickerLabel.setFont(new Font("arial",Font.BOLD,24));
        textFieldPanel.add(tickerLabel);
        textFieldPanel.add(inputStock);        
        totalGUI.add(textFieldPanel);
        
        
        //ADD THE TABLE HERE
        JPanel jTablePanel = new JPanel();
        jTablePanel.setBackground(Color.white);
        jTablePanel.setLocation(220,70);
        jTablePanel.setSize(500,160);
        
        tp = new TablePane(customerId);
        jTablePanel.add(tp.createContentPane());        
        totalGUI.add(jTablePanel);
        
        
         //ADD THE TABLE HERE
        JPanel removePane = new JPanel();
        removePane.setBackground(Color.white);
        removePane.setLocation(10,70);
        removePane.setSize(200,160);
        removeStockButton = new JButton("Remove");
        removeStockButton.setBackground(Color.white);
        addStockButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actionAddPerformed(evt);
            }
        });
        
        removeStockButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actionRemovePerformed(evt);
            }
        });
        
        removePane.add(removeStockButton);
        
        totalGUI.add(removePane);
        
        // Finally we return the JPanel.
        totalGUI.setOpaque(true);
        
        return totalGUI;
    }
    
    
    /*
     *Action Performed for addStock button
     *
     */
    public void actionAddPerformed(ActionEvent e) {
       
       tp.addRow(inputStock.getText().toUpperCase());
       
       c = Connector.myConnector;
       c.addToWatchList(customerId,inputStock.getText().toUpperCase());
       
       updateUI();       
    }
    
    
    public void actionRemovePerformed(ActionEvent e) {
        
        tp.removeRow(inputStock.getText().toUpperCase());       
        c = Connector.myConnector;
        c.removeFromWatchList(customerId,inputStock.getText().toUpperCase());
        
        updateUI();
       
    }
    
    public void updateUI(){
        totalGUI.updateUI();
        
    }
    
    
}
